<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      color: rgb(228, 231, 233);
    }

    .box {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;

      width: 250px;
      height: 250px;
      background-color: rgb(158, 13, 37);
      margin: 50px auto;
      border-radius: 20px;
    }

    p {
      /* width: 200px; */

      text-align: center;
      /* background-color: pink; */
    }

    h2 {
      width: 200px;
      /* background-color: #fff; */
      text-align: center;
    }

    h3 {
      /* width: 200px; */
      margin-top: -10px;
      /* background-color: #fff; */
      text-align: center;
      font-size: 10px;
    }

    h3 span {
      display: inline-block;
      width: 40px;
      background-color: #424242;
      line-height: 30px;
      font-size: 20px;
      border-radius: 5px;
      transition: all .5s;
    }

    h4 {
      width: 200px;
      /* background-color: #fff; */
      text-align: center;
    }
  </style>
</head>

<body>
  <div class="box">
    <p class="now">今天是</p>
    <h2>倒计时</h2>
    <h3>
      <span class="d">00</span> 天
      <span class="h">00</span> 时
      <span class="m">00</span> 分
      <span class="s">00</span> 秒
    </h3>
    <h4>距2025/1/6 18:00</h4>
  </div>

  <script>
    const date = new Date()
    document.querySelector('p').innerHTML = `今天是${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
    const yan = document.querySelector('span.s')

    setInterval(function () {
      let h = (new Date('2025-1-8 23:00:00') - Date.now()) / 1000
      // console.log(h);

      let dd = parseInt(h / 60 / 60 / 24)
      let hh = parseInt(h / 60 / 60 % 24)
      let mm = parseInt(h / 60 % 60)
      let ss = parseInt(h % 60)
      dd = dd < 10 ? '0' + dd : dd
      hh = hh < 10 ? '0' + hh : hh
      mm = mm < 10 ? '0' + mm : mm
      ss = ss < 10 ? '0' + ss : ss
      document.querySelector('.d').innerHTML = dd
      document.querySelector('.h').innerHTML = hh
      document.querySelector('.m').innerHTML = mm
      document.querySelector('.s').innerHTML = ss

    }, 1000)

    //随机颜色函数
    function getRandomColor(flag = true) {
      if (flag) {
        // 3. 如果是true 则返回 #ffffff
        let str = '#'
        let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
        // 利用for循环随机抽6次 累加到 str里面
        for (let i = 1; i <= 6; i++) {
          // 每次要随机从数组里面抽取一个  
          // random 是数组的索引号 是随机的
          let random = Math.floor(Math.random() * arr.length)
          // str = str + arr[random]
          str += arr[random]
        }
        return str

      } else {
        // 4. 否则是 false 则返回 rgb(255,255,255)
        let r = Math.floor(Math.random() * 256)  // 55
        let g = Math.floor(Math.random() * 256)  // 89
        let b = Math.floor(Math.random() * 256)  // 255
        return `rgb(${r},${g},${b})`
      }
    }

  </script>
</body>

</html>